home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / njfrer.arc / NJFRERAM.ASM next >
Encoding:
Assembly Source File  |  1987-05-07  |  11.4 KB  |  497 lines

  1.  
  2. ;
  3. ;   Nifty James' FREE RAM program
  4. ;   Version 1.13 of 7 May 1987
  5. ;
  6. ;   Copyright 1987 by Mike Blaszczak, all Rights Reserved.
  7. ;
  8. ;   Shareware  $7
  9.  
  10. ;   ASCII Characters
  11.  
  12. tab        equ    9
  13. lf        equ    10
  14. cr        equ    13
  15. space        equ    32
  16.  
  17. ;   IBM Specific Keyboard Scan-codes
  18.  
  19. AltDown        equ    56        ; ALT scancode
  20. AltUp        equ    56 + 80h
  21. RShiftDown    equ    54        ; right-shift scancode
  22. RShiftUp    equ    54 + 80h
  23.  
  24. ;   BIOS Interrupts
  25.  
  26. Timer        equ    008h        ; BIOS 18.2Hz function
  27. Keyboard    equ    009h        ; BIOS keypress call
  28. Video        equ    010h        ; BIOS video function
  29. DiskService    equ    013h        ; BIOS/XT BIOS disk service
  30. TickTock    equ    01Ch        ; the BIOS timer trap
  31. SnagDOS        equ    028h        ; a function to snag DOS by
  32. SigVec        equ    0D0h        ; vector to point to our signature
  33.  
  34. ;   I/O Ports
  35.  
  36. NMIPort        equ    020h        ; the Interrupt controller port
  37. KeyPort        equ    060h        ; the keyboard's I/O port
  38.  
  39. ;   DOS Calls
  40.  
  41. SetVec        equ    025h        ; call to set an int vector
  42. KEEP        equ    031h        ; call to Terminate and Stay Res
  43. Critical    equ    034h        ; call to get the DOS critical flag
  44. GetVec        equ    035h        ; call to get an int vector
  45. Allocate    equ    048h        ; call to allocate memory
  46. Terminate    equ    04Ch        ; call to Terminate
  47.  
  48.  
  49.  ;   Public declarations for SYMDEB
  50.  
  51. PUBLIC First
  52. PUBLIC StackArea, StackTop, SaveAX, SaveSS, SaveSP, ClockVec, ClockVecO
  53. PUBLIC ClockVecS, Display, DisplayOff, DisplaySeg, DisplayPort, CriticalFlag
  54. PUBLIC CriticalFlagO, CriticalFlagS, OTT, OTTO, OTTS, OKeyVec, OKeyVecO
  55. PUBLIC OKeyVecS, OSnagger, OSnaggerO, OSnaggerS, ODiskCall, ODiskCallO
  56. PUBLIC ODiskCallS, WorkFlag, UpdateFlag, AltFlag, ShiftFlag, Ticks, InDisk
  57. PUBLIC InThere, DiskTrap, TCTrap, TCDone, TimeTrap, NotTime, KeyTrap
  58. PUBLIC TryUpAlt, TryDnShift, TryUpShift, TripOut, KeyOut, Signature, WorkTrap
  59. PUBLIC WorkHard, NoWork, DoWork, CalcLoop, Convert2, Convert3, IsMono
  60. PUBLIC SynchLow, SynchHigh, PutUp, GetOut, CalcArea, AnswerArea, DebugPlace
  61. PUBLIC OverResident, Already, Exit, InstallMe, NotMono, Banner
  62. PUBLIC Installed, Failed
  63.  
  64.  
  65. CSeg        segment    para public 'CODE'
  66.         assume    ds:CSeg,ss:CSeg,cs:CSeg
  67.  
  68.         org    100h
  69.  
  70. extrn   Show:near    ; from NJLIB
  71.  
  72. First:        jmp    OverResident
  73.         nop
  74.  
  75. StackArea    dw    199 dup(0DEADh)    ; the stack for interrupts
  76. StackTop    dw    0DEADh
  77.  
  78. Signature    db    'NJ'        ; the signature
  79.  
  80. SaveAX        dw    0        ; save the AX here
  81. SaveSS        dw    0        ; save the SS and SP here
  82. SaveSP        dw    0
  83.  
  84.  
  85. ClockVec    label    dword        ; the original clock tick vector
  86. ClockVecO    dw    0
  87. ClockVecS    dw    0
  88.  
  89. Display        label    dword        ; pointer to display storage
  90. DisplayOff    dw    62*2
  91. DisplaySeg    dw    0B800h        ; assume the color card
  92. DisplayPort    dw    03DAh        ; display status port address
  93.  
  94. CriticalFlag    label    dword        ; the DOS critical flag
  95. CriticalFlagO    dw    0
  96. CriticalFlagS    dw    0
  97.  
  98. OTT        label    dword        ; the original BIOS Timer Trap
  99. OTTO        dw    0
  100. OTTS        dw    0
  101.  
  102. OKeyVec        label    dword        ; the original keypress vector
  103. OKeyVecO    dw    0
  104. OKeyVecS    dw    0
  105.  
  106. OSnagger    label    dword        ; the original DOS int 28h
  107. OSnaggerO    dw    0
  108. OSnaggerS    dw    0
  109.  
  110. ODiskCall    label    dword        ; the disk services place
  111. ODiskCallO    dw    0
  112. ODiskCallS    dw    0
  113.  
  114. WorkFlag    dw    0        ; semaphore; 1 if we should display
  115. UpdateFlag    dw    0        ; semaphore; 1 if we should update
  116.  
  117. AltFlag        db    0        ; set if ALT is pressed
  118. ShiftFlag    db    0        ; set if RShift is pressed
  119.  
  120. Ticks        db    0        ; tick count for timer
  121. InDisk        db    0        ; semaphore; 1 if in disk control
  122.  
  123. InThere        db    0        ; recursiveness flag
  124.  
  125. ; ---------------------------------------------------------------------------
  126.  
  127. DiskTrap:    inc    CS:InDisk    ; set disk handler flag
  128.         pushf
  129.         call    CS:[ODiskCall]    ; call the disk handler
  130.         pushf
  131.         dec    CS:InDisk    ; reset disk handler flag
  132.         popf
  133.         iret            ; return!
  134.  
  135. ; ---------------------------------------------------------------------------
  136.  
  137. TCTrap:        pushf
  138.         call    CS:[OTT]    ; call the original
  139.         pushf
  140.         cli
  141.         cmp    CS:[WorkFlag],0
  142.         je    TCDone
  143.         push    es
  144.         push    bx        ; check the critical flag
  145.         les    bx,CS:[CriticalFlag]
  146.         cmp    byte ptr es:[bx],0
  147.         pop    bx
  148.         pop    es
  149.         jne    TCDone
  150.  
  151.         cmp    CS:[InDisk],0    ; and the disk service flag
  152.         jne    TCDone
  153.  
  154.         jmp    WorkHard
  155.  
  156. TCDone:        popf
  157.         iret
  158.  
  159. ; ---------------------------------------------------------------------------
  160.  
  161. TimeTrap:    pushf            ; save flags
  162.         push    ax
  163.  
  164.         inc    CS:Ticks    ; increment ticks count
  165.         mov    al,CS:Ticks    ; is it time?
  166.         cmp    al,18
  167.         jne    NotTime        ; no, not yet
  168.  
  169.         xor    ax,ax        ; yes, zero time count
  170.         mov    CS:Ticks,al
  171.         mov    CS:UpdateFlag,1 ; set update flag
  172.  
  173. NotTime:    pop    ax
  174.         popf
  175.         jmp    CS:[ClockVec]
  176.         
  177. ; ---------------------------------------------------------------------------
  178.  
  179. KeyTrap:    pushf
  180.         push    ax
  181.  
  182.         in    al,KeyPort    ; get key from keyboard
  183.         cmp    al,AltDown    ; was ALT pressed?
  184.         jne    TryUpAlt    ; no.  Maybe it was released?
  185.  
  186.         mov    CS:AltFlag,1    ; yeah!  It was pressed, set flag
  187.         jmp    short TripOut    ; and flow through
  188.  
  189. TryUpAlt:    cmp    al,AltUp    ; was ALT released?
  190.         jne    TryDnShift    ; no, maybe it was shift?
  191.  
  192.         mov    CS:AltFlag,0    ; yeah!  ALT was released
  193.         jmp    short TripOut    ; reset flag and go through
  194.  
  195. TryDnShift:    cmp    al,RShiftDown    ; okay.  Maybe RShifty was pressed?
  196.         jne    TryUpShift    ;  nah, maybe  it was released?
  197.  
  198.         mov    CS:ShiftFlag,1    ; yeah! it was pressed, so set flag
  199.         jmp    short TripOut    ;  and jump through
  200.  
  201. TryUpShift:    cmp    al,RShiftUp    ; well, maybe it was released?
  202.         jne    KeyOut        ;  no, guess not, just pass it thru
  203.  
  204.         mov    CS:ShiftFlag,0    ;  yeah, it was released!
  205.  
  206. TripOut:    mov    al,CS:AltFlag    ; so, is it both ALT and RSHIFT?
  207.         and    al,CS:ShiftFlag
  208.         je    KeyOut        ; no, just pass thru
  209.         not    CS:WorkFlag    ;  yeah, it was!  toggle work flag
  210.  
  211. KeyOut:        pop    ax
  212.         popf
  213.  
  214.         jmp    CS:[OKeyVec]        
  215.         
  216. ; ---------------------------------------------------------------------------
  217.  
  218.  
  219. WorkTrap:    pushf            ; simulate a INT
  220.         call    CS:[OSnagger]
  221.         pushf
  222.  
  223. WorkHard:    cmp    CS:[WorkFlag],0        ; are we activated?
  224.         je    NoWork            ;  no, don't do work
  225.  
  226.         cmp    CS:[UpdateFlag],0    ; is it time to update?
  227.         je    NoWork            ;  no, don't do work
  228.  
  229.         cmp    CS:[InThere],0        ; are we getting recursive?
  230.         jne    NoWork            ;  yes, don't do work
  231.  
  232.         call    DoWork
  233. NoWork:        popf
  234.         iret        
  235.  
  236.  
  237. DoWork:        cli            ; turn off interrupts!
  238.         cld            ; and set direction up!!!
  239.         mov    CS:SaveAX,ax    ; save the stack seg from call
  240.         mov    CS:SaveSP,sp
  241.         mov    CS:SaveSS,ss
  242.  
  243.         mov    ax,cs            ; point to our local
  244.         mov    ss,ax            ;  stack segment
  245.         mov    ax,offset StackTop
  246.         mov    sp,ax
  247.  
  248.         push    bx        ; save all the regs we use
  249.         push    cx
  250.         push    dx
  251.         push    si
  252.         push    di
  253.         push    ds
  254.         push    es
  255.  
  256.         mov    ax,cs
  257.         mov    ds,ax        ; establish data seg addressing
  258.         mov    es,ax
  259.  
  260.         inc    InThere        ; don't be recursive
  261.         mov    UpDateFlag,0    ;  and don't do the work twice!
  262.  
  263.         cli            ; reallow interrupts for now
  264.  
  265.         mov    ah,Allocate    ; try to get a major amount of mem
  266.         mov    bx,0FFFFh
  267.         int    21h        ; to see how much is free
  268.  
  269.         xor    ax,ax        ; zero the AX
  270.         xchg    ax,bx        ; now, AX = paragraphs and BX = 0
  271.         mov    cx,4        ; get # of bytes
  272. CalcLoop:    rcl    ax,1        ; multiply by two
  273.         rcl    bx,1        ; add in carry
  274.         loop    CalcLoop
  275.  
  276.     ; now, BX:AX contains bytes of free memory
  277.  
  278.         mov    CalcArea,ax        ; store bytes
  279.         mov    CalcArea+2,bx
  280.  
  281.         mov    di,offset AnswerArea
  282.         mov    al,space
  283.         mov    cx,7
  284.     repnz    stosb
  285.  
  286.     ; now, convert the number of bytes into ASCII, and store at
  287.     ; AnswerArea
  288.  
  289.         mov    si,offset CalcArea
  290.         dec    di
  291.  
  292. Convert2:    push    si            ; save source pointer
  293.  
  294.         xor    bx,bx            ; done flag
  295.         mov    cx,2            ; 2 words in number
  296.         xor    dx,dx            ; previous remainder
  297.         add    si,2            ; point to high end
  298.  
  299. Convert3:    push    cx            ; save count
  300.         mov    ax,[si]            ; get 16-bit digit
  301.         mov    cx,10            ; divisor of 10
  302.         div    cx            ; divide
  303.         mov    [si],ax            ; put 16-bit digit back
  304.         or    bx,ax            ; check for zero
  305.         sub    si,2            ; point to next 16-bit digit
  306.         pop    cx            ; restore count
  307.         loop    Convert3        ; do more
  308.  
  309.         or    dl,'0'        ; make remainder into decimal digit
  310.         mov    [di],dl        ; store it
  311.         dec    di        ; and fix pointer
  312.  
  313.         pop    si        ; restore source pointer
  314.         cmp    bx,0        ; was that the end?
  315.         jnz    Convert2    ; no, do more!
  316.  
  317.         mov    ah,15
  318.         int    Video
  319.         mov    cx,0B000h
  320.         cmp    al,7
  321.         je    IsMono
  322.         mov    cx,0B800h
  323.         add    ch,bh        ; add the current page to it
  324.         
  325. IsMono:        mov    DisplaySeg,cx
  326.  
  327.         les    di,Display    ; stuff it into the display!
  328.         mov    si,offset AnswerArea
  329.         mov    ah,7        ; attribute
  330.         mov    cx,18        ; 17 characters in the message:
  331.                     ; "xxxxxxx Bytes Free"
  332.  
  333.         mov    dx,DisplayPort    ; get the status port number
  334.         and    dx,dx        ;  or is it mono?
  335.         je    PutUp        ;  yeah, skip synchro
  336.  
  337. SynchLow:                ; wait for the synch to go low
  338.         in    al,dx
  339.         test    al,8
  340.         jne    SynchLow
  341.  
  342. SynchHigh:                ; wait for start of overscan
  343.         in    al,dx
  344.         test    al,8
  345.         je    SynchHigh
  346.     
  347. PutUp:        lodsb            ; load a byte of the message
  348.         stosw            ; store a word into the display
  349.         loop    PutUp        ; do the rest
  350.  
  351. GetOut:        cli            ; disallow interrupts for crucial code
  352.         dec    InThere        ; clear work flag
  353.         pop    es
  354.         pop    ds        ; reset the registers
  355.         pop    di
  356.         pop    si
  357.         pop    dx
  358.         pop    cx
  359.         pop    bx
  360.  
  361.         mov    sp,CS:SaveSP        ; restore the original stack
  362.         mov    ss,CS:SaveSS
  363.         mov    ax,CS:SaveAX
  364.  
  365.         sti                ; re-allow ints and
  366.         ret            ; return to caller
  367.  
  368. CalcArea    dw    2 dup(0)
  369.  
  370. AnswerArea    db    7 dup (' ')
  371. DebugPlace    db    " Bytes Free"
  372.  
  373. ; ---------------------------------------------------------------------------
  374.  
  375. OverResident:    mov    dx,offset Banner    ; print the banner
  376.         mov    cx,BannerLen
  377.         call    Show
  378.  
  379.         mov    ah,GetVec        ; see if already present
  380.         mov    al,SigVec
  381.         int    21h
  382.  
  383.         mov    ax,ES:[BX]        ; see if the signature
  384.         cmp    ax,'JN'            ;  is there?
  385.         jne    InstallMe
  386.  
  387. Already:    mov    dx,offset Failed    ; already installed!
  388.         mov    cx,FailedLen
  389.         call    Show
  390.  
  391. Exit:        mov    ah,Terminate        ; terminate with ERRORLEVEL
  392.         mov    al,1
  393.         int    21h
  394.  
  395. InstallMe:    mov    ah,Critical        ; get the DOS critical flag
  396.         int    21h
  397.         mov    CriticalFlagO,bx
  398.         mov    CriticalFlagS,es
  399.  
  400.         mov    ah,GetVec        ; handle the tick-tock
  401.         mov    al,TickTock
  402.         int    21h
  403.         mov    OTTO,bx
  404.         mov    OTTS,es
  405.  
  406.         mov    ah,SetVec
  407.         mov    al,TickTock
  408.         mov    dx,offset TCTrap
  409.         int    21h
  410.  
  411.         mov    ah,GetVec
  412.         mov    al,DiskService
  413.         int    21h
  414.         mov    ODiskCallO,bx
  415.         mov    ODiskCallS,es
  416.     
  417.         mov    ah,GetVec
  418.         mov    al,SnagDOS        ; get originial INT 028h
  419.         int    21h
  420.         mov    OSnaggerO,bx
  421.         mov    OSnaggerS,es
  422.     
  423.         mov    ah,GetVec        ; get the current keyboard vec
  424.         mov    al,Keyboard
  425.         int    21h
  426.  
  427.         mov    OKeyVecO,bx
  428.         mov    OKeyVecS,es
  429.  
  430.         mov    ah,GetVec        ; get the clock-tick vec
  431.         mov    al,Timer
  432.         int    21h
  433.  
  434.         mov    ClockVecO,bx        ; store clock vector
  435.         mov    ax,es
  436.         mov    ClockVecS,ax
  437.  
  438.         mov    ah,15        ; call BIOS to get video mode
  439.         int    Video
  440.         cmp    al,7        ; is it mono?
  441.         jne    NotMono
  442.  
  443.         mov    ax,0B000h    ; yes, change it to mono
  444.         mov    DisplaySeg,ax    
  445.         mov    ax,0
  446.         mov    DisplayPort,ax
  447.  
  448. NotMono:    mov    ah,SetVec        ; set an int vector
  449.         mov    al,Timer        ;  the timer's
  450.         mov    dx,offset TimeTrap    ; to our routine
  451.         int    21h
  452.  
  453.         mov    ah,SetVec        ; get the keypress vector
  454.         mov    al,Keyboard
  455.         mov    dx,offset KeyTrap
  456.         int    21h
  457.  
  458.         mov    ah,SetVec        ; get the snag DOS vector
  459.         mov    al,SnagDOS
  460.         mov    dx,offset WorkTrap
  461.         int    21h
  462.  
  463.         mov    ah,SetVec        ; set the signature pointer
  464.         mov    al,SigVec
  465.         mov    dx,offset Signature
  466.         int    21h
  467.  
  468.         mov    dx,offset Installed
  469.         mov    cx,InstalledLen
  470.         call    Show
  471.  
  472.         mov    ah,KEEP        ; terminate and stay resident
  473.         mov    al,0    
  474.         mov    dx,offset OverResident
  475.         add    dx,15
  476.         mov    cx,4
  477.         shr    dx,cl
  478.         int    21h
  479.  
  480. ; ---------------------------------------------------------------------------
  481.  
  482. Banner        db    cr,lf,"Nifty James' FREE RAM program",cr,lf
  483.         db    "Version 1.13 of 07 May 1987",cr,lf,lf
  484. BannerLen    equ    this byte - Banner
  485.  
  486. Installed    db    cr,lf,"NJFRERAM is installed!",cr,lf
  487.         db    "Press <Alt>+<RightShift> to toggle the display",cr,lf,lf
  488. InstalledLen    equ    this byte - Installed
  489.  
  490. Failed        db    cr,lf,"NJFRERAM was already present!",cr,lf,lf
  491. FailedLen    equ    this byte - Failed
  492.  
  493. CSeg        ends
  494.         end    First
  495.  
  496.